using UnityEngine;
using System.Collections;
// http://wiki.unity3d.com/index.php?title=Xbox360Controller

[AddComponentMenu("TagMachine/Entity/Moving Entity")]
[RequireComponent (typeof (NavMeshAgent))]
public class MovingEntity : MonoBehaviour
{	
	public int NumPlayer				{ get; set; }
	public bool IsMoving 				{ get { return NavAgent.velocity.sqrMagnitude > 0.001f; } }

	public NavMeshAgent NavAgent 		{ get; private set; }
	private float m_SpeedModifier 		= 1.0f;

	private string m_HorizontalAxis 	= "Horizontal_Key";
	private string m_VerticalAxis 		= "Vertical_Key";
	
	// Use this for initialization
	void Awake ()
	{
		NumPlayer 		= 0;
		NavAgent 		= GetComponent< NavMeshAgent >();
		NavAgent.updateRotation = false;
	}

	void Start()
	{
		if ( (!GameModeClassic.Exists || !GameModeClassic.Instance.OnlyControllers) &&
			NumPlayer == 0 )
		{
			m_HorizontalAxis 	= "Horizontal_Key";
			m_VerticalAxis 		= "Vertical_Key";
		}
		else
		{
			int AxisNumPlayer = NumPlayer;
			if ( GameModeClassic.Exists &&
				GameModeClassic.Instance.OnlyControllers )
			{
				++AxisNumPlayer;
			}

			m_HorizontalAxis 	= "L_XAxis_" + AxisNumPlayer;
			m_VerticalAxis 		= "L_YAxis_" + AxisNumPlayer;
		}
	}
	
	public void UpdateSpeed( float _SpeedModifier )
	{
		m_SpeedModifier = _SpeedModifier;
	}
	
	public void UpdateSize( CapsuleCollider _Collider )
	{
		// the capsule needs to be bigger for collision
		NavAgent.radius = _Collider.radius * transform.localScale.x - 0.01f;
		NavAgent.height = _Collider.height * transform.localScale.x;
	}
	
	// Update is called once per frame
	void Update () 
	{
		Vector3 Move 	= Vector3.zero;
		float Speed 	= m_SpeedModifier * NavAgent.speed;

		Move.x 			= Input.GetAxis( m_HorizontalAxis );
		Move.z 			= Input.GetAxis( m_VerticalAxis );
		
		float VelMag 	= NavAgent.velocity.sqrMagnitude;
		float MoveMag 	= Move.sqrMagnitude;

		if ( MoveMag > 1.0f ) 
		{
			MoveMag = 1.0f;
			Move.Normalize();
		}

		if ( VelMag < 0.001f && MoveMag < 0.001f ) 
		{
			NavAgent.velocity 		= Vector3.zero;
		} 
		else 
		{
			if ( MoveMag < 0.001f )
			{
				// use the stoppingDistance as deceleration modifier
				NavAgent.velocity -= NavAgent.velocity.normalized * NavAgent.acceleration * NavAgent.stoppingDistance * Time.deltaTime;

				// If the velocity points behind the entity, this means we'll stop
				if ( Vector3.Dot( NavAgent.velocity, transform.forward ) < 0.0f )
				{
					NavAgent.velocity = Vector3.zero;
				}
			}
			else
			{
				// If the move points behind the entity, this means we decelerate
				float Acceleration 	= NavAgent.acceleration;
				if ( Vector3.Dot( Move, transform.forward ) < 0.0f )
				{
					Acceleration 	= NavAgent.acceleration * NavAgent.stoppingDistance;
				}

				NavAgent.velocity 	+= Move * Acceleration * Time.deltaTime;
				if ( NavAgent.velocity.sqrMagnitude > 0.001f )
				{
					transform.rotation 	= Quaternion.LookRotation( NavAgent.velocity.normalized );
				}
			}

			if ( NavAgent.velocity.magnitude > Speed ) 
			{
				NavAgent.velocity = NavAgent.velocity.normalized * Speed;
			}
		}
	}
}
 